package com.example.finalframe.service.impl;

import com.example.finalframe.common.Constant;
import com.example.finalframe.dao.UserDao;
import com.example.finalframe.model.dto.*;
import com.example.finalframe.model.entity.Menu;
import com.example.finalframe.model.entity.Role;
import com.example.finalframe.model.entity.UserRole;
import com.example.finalframe.service.intf.UserService;
import com.example.finalframe.utils.Base64Util;
import com.example.finalframe.utils.RedisUtil;
import com.example.finalframe.utils.TokenUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    RedisUtil redisUtil;

    @Autowired
    UserDao userDao;

//    @Override
//    public UserRoleDto findUserRole(Integer id) {
//        UserRole userRole = userDao.findUserRoleByUserId(id);
//        if(userRole!=null){
//            UserRoleDto userRoleDto = new UserRoleDto();
//            BeanUtils.copyProperties(userRole, userRoleDto);
//            return userRoleDto;
//        }
//        return null;
//    }
//
//    @Override
//    public RoleDto findRoleByRoleId(Integer roleId) {
//        Role role = userDao.findRoleByRoleId(roleId);
//        if(role!=null){
//            RoleDto roleDto = new RoleDto();
//            BeanUtils.copyProperties(role, roleDto);
//            return roleDto;
//        }
//        return null;
//    }
//
//    @Override
//    public RolePermissionDto findRolePermissionByRoleId(Integer roleId) {
//        RolePermission rolePermission = userDao.findRolePermissionByRoleId(roleId);
//        if(rolePermission!=null){
//            RolePermissionDto rolePermissionDto = new RolePermissionDto();
//            BeanUtils.copyProperties(rolePermission, rolePermissionDto);
//            return rolePermissionDto;
//        }
//        return null;
//    }
//
//    @Override
//    public PermissionDto findPermissionByPermissionId(Integer permissionId) {
//        Permission permission = userDao.findPermissionByPermissionId(permissionId);
//        if(permission!=null){
//            PermissionDto permissionDto = new PermissionDto();
//            BeanUtils.copyProperties(permission, permissionDto);
//            return permissionDto;
//        }
//        return null;
//    }

    @Override
    public UserRoleDto findUserRole(Integer id) {
        return null;
    }

    @Override
    public UserRoleDto findUserRoleByUserId(Integer userId) {
        UserRole userRole = userDao.findUserRoleByUserId(userId);
        if(userRole!=null){
            UserRoleDto userRoleDto = new UserRoleDto();
            System.out.println("userroleId"+userRole.getRoleid());
            BeanUtils.copyProperties(userRole, userRoleDto);
            return userRoleDto;
        }
        return null;
    }

    @Override
    public RoleDto findRoleByUserId(Integer userId) {
        UserRoleDto userRoleDto = this.findUserRoleByUserId(userId);
        System.out.println("buweik");
        if(userRoleDto!=null){
            Integer roleId = userRoleDto.getRoleid();
            System.out.println("roleId"+roleId);
            Role role = userDao.findRoleByRoleId(roleId);
            if(role != null) {
                RoleDto roleDto = new RoleDto();
                BeanUtils.copyProperties(role, roleDto);
                return roleDto;
            }
        }
        return null;
    }

    @Override
    public RoleDto findRoleMenuByRoleId(Integer roleId) {
        return null;
    }

    @Override
    public RoleMenuDto findMenuByMenuId(Integer menuId) {
        return null;
    }


    @Override
    public Integer findRoleId(Integer id) {
        Integer RoleId = userDao.findRoleIdByUserId(id);
        if(RoleId!=null){
            UserRoleDto userRoleDto = new UserRoleDto();
            BeanUtils.copyProperties(RoleId, userRoleDto);
            return RoleId;
        }
        return null;
    }
    @Override
    public boolean checkToken(long userid, String token, String clientType) {
        if(clientType==null || clientType.isEmpty()){
            clientType = Constant.CLIENT_TYPE_WEB;
        }
        Map<Object,Object> map = redisUtil.hmget(Constant.TOKEN_KEY_PREFIX +userid);
        if(map!=null){
            if(token.equals(map.get(clientType))) {
                return true;
            }
        }
        return false;
    }

    @Override
    public String genarateToken(Integer userId, String password, String clientType) throws UnsupportedEncodingException {
        String tokenValue = TokenUtil.generateTokenValue(clientType,userId,password);
        Map<String,Object> map = new HashMap<>();
        map.put(clientType,tokenValue);
        redisUtil.hmset("token:"+userId,map);

        String token = userId+"-"+tokenValue;
        return Base64Util.encode(token);
    }

    @Override
    public RoleDto findRoleByRoleId(Integer roleId) {
        Role role = userDao.findRoleByRoleId(roleId);
        if(role!=null){
            RoleDto roleDto = new RoleDto();
            BeanUtils.copyProperties(role, roleDto);
            return roleDto;
        }
        return null;
    }

    @Override
    public List<MenuDto> findMenuListByRoleId(Integer roleid) {
        List<Menu> menuList = userDao.findMenuListByRoleId(roleid);
        return getMenuDtos(menuList);
    }

    @Override
    public List<MenuDto> findMenuList() {
        List<Menu> menuList = userDao.findMenuList();
        return getMenuDtos(menuList);
    }

    private List<MenuDto> getMenuDtos(List<Menu> menuList) {
        if(menuList!=null) {
            List<MenuDto> menuDtoList = new ArrayList<MenuDto>();
            for(Menu m : menuList) {
                MenuDto menuDto = new MenuDto();
                BeanUtils.copyProperties(m, menuDto);
                menuDtoList.add(menuDto);
            }
            return menuDtoList;
        }
        return null;
    }

    @Override
    public Boolean addMenuForRole(Integer roleid, Integer menuid) {
        return userDao.addMenuForRole(roleid,menuid);
    }

    @Override
    public Boolean deleteMenuForRole(Integer roleid, Integer menuid) {
        return userDao.deleteMenuForRole(roleid,menuid);
    }
}
